# encoding: utf-8
import os
from filecmp import dircmp
import click
from automatic.file.generator.FolderCompareLinuxGenerator import FolderCompareLinuxGenerator
from automatic.file.generator.FolderCompareUpdateFileGenerator import FolderCompareUpdateFileGenerator
from automatic.file.generator.FolderCompareWindowsGenerator import FolderCompareWindowsGenerator
from automatic.file.FolderCompareResultType import FOLDER_COMPARE_RESULT_TYPE_DIFFERENT_FILE, \
    FOLDER_COMPARE_RESULT_TYPE_NEW_ONLY, FOLDER_COMPARE_RESULT_TYPE_OLD_ONLY

def process_listener(listeners, result_type,
                     new_root_dir, new_relative_dir, old_root_dir, old_relative_dir, file_name):
    for listener in listeners:
        listener.process(result_type,new_root_dir,new_relative_dir,old_root_dir,old_relative_dir,file_name)

def print_diff_files(dcmp, listeners,new_root_dir,old_root_dir):

    for name in dcmp.diff_files:
        new_relative_dir = dcmp.left[len(new_root_dir):]
        old_relative_dir = dcmp.right[len(old_root_dir):]
        process_listener(listeners, FOLDER_COMPARE_RESULT_TYPE_DIFFERENT_FILE, new_root_dir,
                         new_relative_dir,old_root_dir, old_relative_dir, name)

    for name in dcmp.left_only:
        new_relative_dir = dcmp.left[len(new_root_dir):]
        old_relative_dir = dcmp.right[len(old_root_dir):]
        process_listener(listeners, FOLDER_COMPARE_RESULT_TYPE_NEW_ONLY, new_root_dir,
                         new_relative_dir, old_root_dir, old_relative_dir, name)

    for name in dcmp.right_only:
        new_relative_dir = dcmp.left[len(new_root_dir):]
        old_relative_dir = dcmp.right[len(old_root_dir):]
        process_listener(listeners, FOLDER_COMPARE_RESULT_TYPE_OLD_ONLY, new_root_dir,
                         new_relative_dir, old_root_dir, old_relative_dir, name)

    for sub_dcmp in dcmp.subdirs.values():
        print_diff_files(sub_dcmp, listeners, new_root_dir, old_root_dir)

@click.command(help="""对比新旧目录, 生成更新目录和文件, 同时生成更新脚本文件\n
    FolderComparePackage 新目录  旧目录 输出目录""")
@click.argument('new_dir', type=click.STRING)
@click.argument('old_dir', type=click.STRING)
@click.argument('output_dir', type=click.STRING)
@click.pass_context
def do_folder_compare(ctx,new_dir, old_dir, output_dir):

    print("BEGIN FolderComparePackage")
    if not new_dir.endswith(os.sep):
        new_dir += os.sep

    if not old_dir.endswith(os.sep):
        old_dir += os.sep

    if not output_dir.endswith(os.sep):
        output_dir += os.sep

    dcmp = dircmp(new_dir, old_dir)

    win = FolderCompareWindowsGenerator(output_dir+os.sep+"build-update.bat", new_dir, old_dir)
    linux = FolderCompareLinuxGenerator(output_dir+os.sep+"build-update.sh", new_dir, old_dir)
    update = FolderCompareUpdateFileGenerator(output_dir+"WebUpdate"+os.sep, new_dir)
    listeners = [win, linux, update]
    for listener in listeners:
        listener.begin()

    print_diff_files(dcmp, listeners, dcmp.left, dcmp.right)

    for listener in listeners:
        listener.end()

    print("END FolderComparePackage")

if __name__ == '__main__':
    do_folder_compare(obj={})